// -----------------------------------------------------------------------
//  <copyright file="ApiResult.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/8 1:23:42</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Authorization;

namespace DaprPlus.Domain;

/// <summary>
/// API返回结果
/// </summary>
public class ApiResult(ApiResultType type, string? message = null, object? data = null)
    : OsharpResult<ApiResultType>(type, message, data)
{
    public ApiResult() : this(ApiResultType.Success)
    { }

    public ApiResult(string message, ApiResultType type = ApiResultType.Success) : this(type, message)
    { }

    public ApiResult(object data, ApiResultType type = ApiResultType.Success) : this(type, null, data)
    {
        Data = data;
    }


    public bool IsSuccess()
    {
        return ResultType == ApiResultType.Success;
    }


    public bool IsUnauthorized()
    {
        return ResultType == ApiResultType.UnAuth;
    }


    public bool IsForbidden()
    {
        return ResultType == ApiResultType.Forbidden;
    }


    public bool IsNoFound()
    {
        return ResultType == ApiResultType.NotFound;
    }


    public bool IsLocked()
    {
        return ResultType == ApiResultType.Locked;
    }


    public bool IsError()
    {
        return ResultType == ApiResultType.Error;
    }

    public bool IsInfo()
    {
        return ResultType == ApiResultType.Info;
    }

    public static ApiResult Success(string message, object? data = default)
    {
        return new ApiResult(ApiResultType.Success, message, data);
    }

    public static ApiResult Success(object? data = null)
    {
        return data == null ? new ApiResult(ApiResultType.Success) : new ApiResult(data);
    }

    public static ApiResult Error(string message, object? data = default)
    {
        return new ApiResult(ApiResultType.Error, message, data);
    }

    public static ApiResult Info(string? message = null, object? data = default)
    {
        return new ApiResult(ApiResultType.Info, message);
    }

    public static ApiResult NoChanged()
    {
        return new ApiResult(ApiResultType.Info, "操作未引发什么变化。");
    }

    public static ApiResult SuccessOrNoChanged(bool isSuccess, string successMsg)
    {
        return isSuccess
            ? new ApiResult(successMsg)
            : NoChanged();
    }

    public static ApiResult FromAuthorizationResult(AuthorizationResult result)
    {
        var type = result.ResultType switch
        {
            AuthorizationStatus.Ok => ApiResultType.Success,
            AuthorizationStatus.Unauthorized => ApiResultType.UnAuth,
            AuthorizationStatus.Forbidden => ApiResultType.Forbidden,
            AuthorizationStatus.NotFound => ApiResultType.NotFound,
            AuthorizationStatus.Error => ApiResultType.Error,
            _ => ApiResultType.Error
        };
        return new ApiResult(type, result.Message, result.Data);
    }
}

public class ApiResult<TData> : ApiResult
{
    public ApiResult() : this(ApiResultType.Success)
    { }

    public ApiResult(string message, ApiResultType type = ApiResultType.Success) : base(message, type)
    { }

    public ApiResult(object data, ApiResultType type = ApiResultType.Success) : base(data, type)
    { }

    public ApiResult(ApiResultType type, string? message = null, TData? data = default) : base(type, message, data)
    {
        Data = data;
    }

    /// <summary>
    ///  获取或设置 返回数据
    /// </summary>
    public new TData? Data { get; set; }

    public static ApiResult<TData> Success(string message, TData? data = default)
    {
        return new ApiResult<TData>(ApiResultType.Success, message, data);
    }

    public new static ApiResult<TData> Success(object? data = null)
    {
        return data == null ? new ApiResult<TData>(ApiResultType.Success) : new ApiResult<TData>(data);
    }

    public static ApiResult<TData> Error(string message, TData? data = default)
    {
        return new ApiResult<TData>(ApiResultType.Error, message, data);
    }

    public static ApiResult<TData> Info(string? message = null, TData? data = default)
    {
        return new ApiResult<TData>(ApiResultType.Info, message, data);
    }

    public static ApiResult<TData> NoChanged(string? message = null)
    {
        return new ApiResult<TData>(ApiResultType.Info, message ?? "操作未引发什么变化。");
    }
}
